package main

import (
	"encoding/binary"
	"fmt"
	"github.com/golang/protobuf/proto"
	"net"
	proto2 "server/Proto"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:3563")
	if err != nil {
		panic(err)
	}
	hello := proto2.C2S_Login{}
	hello.Account = "yorick"
	hello.Password = "123456"
	data, err := proto.Marshal(&hello)

	// len + data
	m := make([]byte, 4+len(data))

	// 默认使用大端序
	binary.BigEndian.PutUint16(m, 0)
	binary.BigEndian.PutUint16(m, uint16(2+len(data)))
	copy(m[4:], data)
	fmt.Println(m)
	// 发送消息
	conn.Write(m)
	// 接收消息
	readBytes := make([]byte, 2)
	conn.Read(readBytes)
	lenth := binary.BigEndian.Uint16(readBytes)
	readID := make([]byte, 2)
	conn.Read(readID)
	readBody := make([]byte, lenth-2)
	conn.Read(readBody)
	userInfo := proto2.S2C_Login{}
	proto.Unmarshal(readBody, &userInfo)
	fmt.Println(userInfo.User.Name)
	select {}
}
